<?php 
class ControllerDesignBanner extends AdminController {
	
	public function index() {
		M('design/banner');		
		$this->getList();
	}

	public function insert() {
		M('design/banner');
		
		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_design_banner->addBanner($this->request->post);
			
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('design/banner'));
		}

		$this->getForm();
	}

	public function update() {
		M('design/banner');
		
		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_design_banner->editBanner($this->request->get['banner_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('design/banner'));
		}

		$this->getForm();
	}
 
	public function delete() {
		M('design/banner');
		
		if (isset($this->request->post['selected'])) {
			foreach ($this->request->post['selected'] as $banner_id) {
				$this->model_design_banner->deleteBanner($banner_id);
			}
			
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('design/banner'));
		}

		$this->getList();
	}

	public function smartedit() {
		if ($this->hasMP()) {
			$banner_id = (int) G('id');
			$field = G('field');
			$value = G('val');
			$rs = $this->db->update('banner', array($field => $value), "banner_id=$banner_id");
		}
		else $rs = 0;
		$this->response->setOutput(json_encode(array('status' => $rs)));
	}
	
	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);
		
		$qf = new Query_filter();

		$banner_total = $this->model_design_banner->getTotalBanners();		
		$this->data['banners'] = $this->model_design_banner->getBanners($qf);

		$this->data['pagination'] = $this->pagination($banner_total)->render();

		$this->display('design/banner_list.tpl');
	}

	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);
		
		$banner_id = $this->request->get('banner_id', 0);
		
		if ($banner_id) { 
			$this->data['action'] = UA('design/banner/update', 'banner_id=' . $banner_id);
			if (!$this->request->isPost()) {
				$this->vo->data = $this->model_design_banner->getBanner($banner_id);
			}
		} else {
			$this->data['action'] = UA('design/banner/insert');
		}

		$this->data['token'] = $this->session->data['token'];
			
		$this->data['languages'] = C('cache_language_2');
		
		if (isset($this->request->post['banner_image'])) {
			$this->data['banner_images'] = $this->request->post['banner_image'];
		} elseif ($banner_id) {
			$this->data['banner_images'] = $this->model_design_banner->getBannerImages($banner_id);	
		} else {
			$this->data['banner_images'] = array();
		}
	
		$this->display('design/banner_form.tpl');
	}

	protected function validateForm() {
		$this->load->library('form_validation', true);
		$this->form_validation->set_rules('name', '', 'required|range_length[3,64]', $this->_['error_name']);
		
		if (isset($this->request->post['banner_image'])) {
			foreach ($this->request->post['banner_image'] as $banner_image_id => $banner_image) {
				foreach ($banner_image['title'] as $language_id => $title) {
					$this->form_validation->set_rules("banner_image[$banner_image_id][title][$language_id]", '', 'required|range_length[2,64]', $this->_['error_title']);
				}
			}	
		}
		return $this->form_validation->run();
	}
}
?>